package com.itheima.spring.web;

import com.itheima.spring.mapper.UserMapper;
import com.itheima.spring.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.util.Date;

public class controller {



    @Test
   public void test01(){
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(controller.class.getClassLoader().getResourceAsStream("mybatis-config.xml"));

        //创建任务对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = mapper.queryUser(1);
        System.out.println("user = " + user);
    }
    @Test
    public void test02(){
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(controller.class.getClassLoader().getResourceAsStream("mybatis-config.xml"));
        //创建任务对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper1 = sqlSession.getMapper(UserMapper.class);
        Integer integer = mapper1.deleteUser(1);
        System.out.println("integer = " + integer);
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void test03(){
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(controller.class.getClassLoader().getResourceAsStream("mybatis-config.xml"));
        //创建任务对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper2 = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setId(1);
        user.setAddress("上海市闵行区七宝镇");
        user.setAge(19);
        user.setBirthday(new Date());
        user.setSex("男");
        user.setUsername("大壮");
        mapper2.insertUser(user);
        sqlSession.commit();
        sqlSession.close();
    }
}
